package com.fingard.dsp.bank.directbank.cmb05;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.directbank.cmb04.CMB04Base;
import com.fingard.dsp.bank.directbank.cmb04.CMB04Header;
import com.fingard.dsp.bank.directbank.cmb04.CMB04TransCode;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.util.HashMap;
import java.util.Map;

public class CMB05SinglePay extends CMB05Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = d_190802(p_req1908);
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
	
	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

		try {
			StringBuffer sbf = new StringBuffer();
			CMB05Header bankHeader = new CMB05Header();
			bankHeader.transCode = "DCPAYOPR";
			bankHeader.reqSeqID = getQryTransID();
            //组装请求前缀
			writeCommonHeader(sbf,bankHeader);
			//组装请求报文体json
			JSONObject json = new JSONObject();
			HashMap<String,Object> requestMap = new HashMap<String, Object>();
			HashMap<String,String> headMap = new HashMap<String, String>();
			HashMap<String,String> bodyMap = new HashMap<String, String>();
			headMap.put("funcode",bankHeader.transCode);
			headMap.put("userid",bankFront.loginName);
		//	head.put("head",headMap);
			requestMap.put("head",headMap);
			bodyMap.put("modnbr",getBankConfig().getValueAsString("singlePay"));//权限模式编码
			bodyMap.put("refext",p_req1908.singleDetail.askNumber);
			bodyMap.put("sndeac",p_req1908.singleDetail.payAct);
			bodyMap.put("rcveac",p_req1908.singleDetail.recAct);
			bodyMap.put("rcvean",p_req1908.singleDetail.recName);
			bodyMap.put("sysflg","");
			//对公必须上传收方开户行信息
			if(p_req1908.singleDetail.isPrivate!=null && p_req1908.singleDetail.isPrivate.equals("1") && !this.getBankConfig().getValueAsBool("isRecInfo")){
                    bodyMap.put("rcvbrd","");
                    bodyMap.put("rcveab","");
                    bodyMap.put("rcveaa","");
            }else{
                bodyMap.put("rcvbrd",p_req1908.singleDetail.cnapsCode);//
                bodyMap.put("rcveab",p_req1908.singleDetail.recBankName);//
                bodyMap.put("rcveaa",p_req1908.singleDetail.recAreaName);//
            }
            if(p_req1908.singleDetail.paySpd!=null&&p_req1908.singleDetail.paySpd.equals("0")){
                bodyMap.put("buslvl","N");
            }else{
                bodyMap.put("buslvl","Y");
            }
			bodyMap.put("ccynbr","10");//只支持RMB
			bodyMap.put("trsamt",p_req1908.singleDetail.payAmount);
			bodyMap.put("nartxt",p_req1908.singleDetail.memo);//摘要
			bodyMap.put("usetxt",p_req1908.singleDetail.purpose);//用途
			requestMap.put("body",bodyMap);
			json.put("request",requestMap);
			sbf.append(JSON.toJSONString(json));
			String[] tmpStrRet = sendToBank(sbf.toString());

			if (tmpStrRet[0].length() == 0) {
				//把结果转换成json
				JSONObject response = JSON.parseObject(tmpStrRet[1]);
				if (response != null) {
					String info = response.getString("response");
					JSONObject content = JSON.parseObject(info);
					String head = content.getString("head");
					String body = content.getString("body");
					Gson gson = new Gson();
					Map<String, String> headInfo = new HashMap<String, String>();
					headInfo = gson.fromJson(head, headInfo.getClass());
					Map<String, String> bodyInfo = new HashMap<String, String>();
					bodyInfo = gson.fromJson(body, bodyInfo.getClass());
					if(headInfo!=null){
						String responseCode = headInfo.get("resultcode");
						String responseMsg = headInfo.get("resultmsg");
						if(responseCode!=null && responseCode.equals("SUC0000")) {
							retDetail.respCode = "0";
							retDetail.respInfo = "请求提交成功";
							retDetail.reqSeqID = bodyInfo.get("refext");
							retDetail.transState = p_req1908.reqHeader.commandCode + responseCode;
							retDetail.payInfoCode = responseMsg;
						}else{
						    //通讯无异常 不存在超时响应问题 指令提交失败
                            retDetail.respCode = "0";
                            retDetail.respInfo = responseMsg;
                            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                            retDetail.transState = responseCode;
                            retDetail.payInfoCode = responseMsg;
                        }
					}
				} else {
					retDetail.respCode = "-1";
					retDetail.respInfo = "请求未正常返回";
				}
			} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
				retDetail.respCode = "0";
				retDetail.respInfo = tmpStrRet[1];
				retDetail.transState = PaymentState.FAIL_Payment;
				retDetail.payInfoCode = tmpStrRet[0];
			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retDetail.respCode = "-1";
			retDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retDetail;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail(p_req1944);

		try {
			StringBuffer sbf = new StringBuffer();
			CMB05Header bankHeader = new CMB05Header();
			bankHeader.transCode = "DCPAYDTL";
			bankHeader.reqSeqID = getQryTransID();
			//组装请求前缀
			writeCommonHeader(sbf,bankHeader);
			//组装请求报文体json
			JSONObject json = new JSONObject();
			HashMap<String,Object> requestMap = new HashMap<String, Object>();
			HashMap<String,String> headMap = new HashMap<String, String>();
			HashMap<String,String> bodyMap = new HashMap<String, String>();
			headMap.put("funcode",bankHeader.transCode);
			headMap.put("userid",bankFront.loginName);
			//	head.put("head",headMap);
			requestMap.put("head",headMap);
			bodyMap.put("cltref",p_req1944.askNumber);//业务参考号
			requestMap.put("body",bodyMap);
			json.put("request",requestMap);
			sbf.append(JSON.toJSONString(json));
			String[] tmpStrRet = sendToBank(sbf.toString());
            if (tmpStrRet[0].length() == 0) {
				//把结果转换成json
				JSONObject response = JSON.parseObject(tmpStrRet[1]);
				if (response != null) {
					String info = response.getString("response");
					JSONObject content = JSON.parseObject(info);
					String head = content.getString("head");
					String body = content.getString("body");
					Gson gson = new Gson();
					Map<String, String> headInfo = new HashMap<String, String>();
					headInfo = gson.fromJson(head, headInfo.getClass());
					JSONObject bodyInfo = JSON.parseObject(body);
					if(headInfo!=null){
						String responseCode = headInfo.get("resultcode");
						String responseMsg = headInfo.get("resultmsg");
						//判断请求是否成功
						if(responseCode!=null && responseCode.equals("SUC0000")) {
							//根据状态码判断
							Map<String, String> statusMap = new HashMap<String, String>();
							statusMap = gson.fromJson(bodyInfo.getString("stsinf"),statusMap.getClass());
							if(statusMap==null){
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "支付失败";
								tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
								tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + "E";
								tmpRetAtsDetail.payInfoCode = "支付失败";
								retAts.writeRetDetail(tmpRetAtsDetail);
								return retAts;
							}
							String status = statusMap.get("bizsts");
							tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
							tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + status;
							tmpRetAtsDetail.payInfoCode = status;
							if(status.equals("A")){
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "尚未处理";
							}else if(status.equals("B")){
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "已撤销";
							}else if(status.equals("C")){
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "审批拒绝";
							}else if(status.equals("D")){
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "成功";
							}else if(status.equals("E")){
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "失败";
							}else if(status.equals("F")){
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "状态可疑";
							}else {
								tmpRetAtsDetail.respCode = "0";
								tmpRetAtsDetail.respInfo = "请求状态未知";
							}
						}else{
							tmpRetAtsDetail.respCode = "-1";
							tmpRetAtsDetail.respInfo = responseMsg;
						}
					}
				} else {
					tmpRetAtsDetail.respCode = "-1";
					tmpRetAtsDetail.respInfo = "请求未正常返回";
				}
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}